<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>单例模式</title>
</head>
<body>
    <button id="loginBtn">登录</button>
    <button id="loginReg">注册</button>

    <script>
        
        function a(){
            var aa = 123;
            function b(){
                console.log(aa)
            }
            return b
        }

        var res = a()
        res()

        let createDom = function(){
            console.log('123')
            let div = document.createElement('div')
            div.style.display = 'none'
            div.innerHTML = '我是弹窗'
            document.body.appendChild(div)
            return div
        }

        let getSingle = function(fn){
            var result;
            return function(){
                return result || (result = fn.apply(this,arguments))
            }
        }

        let createLogin = getSingle(createDom);
        let createReg = getSingle(createDom)
        // debugger
        document.getElementById('loginBtn').onclick = function(){
            var loginLay = createLogin()
            loginLay.style.display = 'block'
        }

        document.getElementById('loginReg').onclick = function(){
            let loginR = createReg()
            loginR.style.display = 'block'
        }
        

    </script>
</body>
</html>